package com.capgemini.rdlgae.controller

import com.capgemini.rdlgae.domain.*;

class FactureController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [factureInstanceList: Facture.list(params), factureInstanceTotal: Facture.count()]
    }

    def create = {
        def factureInstance = new Facture()
        factureInstance.properties = params
        
		def results = Commande.executeQuery("select distinct a.dateCommande, sum(a.total) from Commande a where a.facture is null group by a.dateCommande order by a.dateCommande desc")
				
		results.each{ item ->
			def commande = new Commande()
			commande.dateCommande = ((List)item).get(0)
			commande.total = ((List)item).get(1)
			factureInstance.addToCommandes(commande)
			factureInstance.total = factureInstance.total.add(commande.total);
		}
		
        return [factureInstance: factureInstance]
    }

    def save = {
        def factureInstance = new Facture(params)
		def commandes = Commande.findAllByFactureIsNull()
		commandes.each{ commande ->
			factureInstance.addToCommandes(commande)
		}		
		
        if (factureInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'facture.label', default: 'Facture'), factureInstance.id])}"
            redirect(action: "show", id: factureInstance.id)
        }
        else {
            render(view: "create", model: [factureInstance: factureInstance])
        }
    }

    def show = {
        def factureInstance = Facture.get(params.id)
		
        if (!factureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
            redirect(action: "list")
        }
        else {
            [factureInstance: factureInstance]
        }
    }

    def edit = {
        def factureInstance = Facture.get(params.id)
        if (!factureInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [factureInstance: factureInstance]
        }
    }

    def update = {
        def factureInstance = Facture.get(params.id)
        if (factureInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (factureInstance.version > version) {
                    
                    factureInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'facture.label', default: 'Facture')] as Object[], "Another user has updated this Facture while you were editing")
                    render(view: "edit", model: [factureInstance: factureInstance])
                    return
                }
            }
            factureInstance.properties = params
            if (!factureInstance.hasErrors() && factureInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'facture.label', default: 'Facture'), factureInstance.id])}"
                redirect(action: "show", id: factureInstance.id)
            }
            else {
                render(view: "edit", model: [factureInstance: factureInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def factureInstance = Facture.get(params.id)
        if (factureInstance) {
            try {
                factureInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'facture.label', default: 'Facture'), params.id])}"
            redirect(action: "list")
        }
    }
}
